<?php
declare (strict_types=1);
/**
 * +----------------------------------------------------------------------
 * | 自定义标签
 * +----------------------------------------------------------------------
 */

namespace addons\Builder\tagLib;

use think\Exception;
use think\template\TagLib;

class Table extends TagLib
{
    /**
     * 标签定义
     *  attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
     * @var array
     */
    protected $tags = array(
        'view'   => ['attr' => 'field,arguments', 'close' => 0],
        'search' => ['attr' => 'search', 'close' => 0],
        'alone'  => ['attr' => 'info,form', 'close' => 0],
    );

    // 这是一个闭合标签的简单演示
    public function tagView($tag)
    {
        try {
            if (!isset($tag['field'])) {
                throw new Exception('缺少field');
            }
            $arguments = isset($tag['arguments']) && $tag['arguments'] ? $tag['arguments'] : '[]';
            $field = $tag['field'];
            $parse = '<?php ';
            $parse .= '$__LIST__ = ' . $field . ';';
            $parse .= 'foreach($__LIST__ as $key=>$item): ';
            $parse .= 'echo \addons\Builder\facade\Builder::TableCache()->createHtml(' . $arguments . ',$item);';
            $parse .= 'endforeach;?>';
            return $parse;
        } catch (\Exception $e) {
            dump($e);
            die;
        }
    }

    public function tagSearch($tag)
    {

        try {
            if (!isset($tag['field'])) {
                throw new Exception('缺少field');
            }
            $field = $tag['field'];
            $parse = '<?php ';
            $parse .= '$__LIST__ = ' . $field . ';';
            $parse .= 'echo \addons\Builder\facade\Builder::TableCache()->getView("search",true,["search" => $__LIST__]);';
            $parse .= '?>';
            return $parse;
        } catch (\Exception $e) {
            dump($e);
            die;
        }
    }

    public function tagAlone($tag)
    {

        try {
            if (!isset($tag['view'])) {
                throw new Exception('缺少view');
            }
            $view = $tag['view'];
            $info = isset($tag['info']) ? $tag['info'] : [];
            $form = isset($tag['form']) ? $tag['form'] : [];
            $parse = '<?php ';
            $parse .= 'echo \addons\Builder\facade\Builder::TableCache()->getView("' . $view . '",true,["info" => ' . $info . ',"form" => ' . $form . ']);';
            $parse .= '?>';
            return $parse;
        } catch (\Exception $e) {
            dump($e);
            die;
        }
    }
}